import asyncio
import aiofiles
import os
from os.path import join

def listdir(path):
	return os.listdir(path)

def path_join(*args):
	return join(*args)

def path_exists(path):
	return os.path.exists(path)

def remove(path):
	return os.remove(path)

async def system(command: str):
	""" async os.system(command) """
	proc = await asyncio.create_subprocess_shell(command)
	await proc.communicate()

def mkdirs(dirname: str):
	try:
		os.makedirs(dirname)
	except Exception as e:
		print(e)
		pass
	return dirname

async def file_read(filename: str, mode="r"):
	async with aiofiles.open(filename, mode) as f:
		return await f.read()

async def copy_file(src, dist):
	""" async os.copy(src, dist) """
	content = await file_read(src, "rb")
	async with aiofiles.open(dist, "wb") as f:
		await f.write(content)

async def rename(src, dist):
	try:
		await copy_file(src, dist)
		remove(src)
	except:
		print(src, dist)

async def copy_folder(src, dist, blacklist):
	tasks = []
	for filename in os.listdir(src):
		if filename in blacklist:
			continue

		if filename.endswith(".json"):
			continue

		task = asyncio.create_task(
			copy_file(f"{src}/{filename}", f"{dist}/{filename}")
		)
		tasks.append(task)

	await asyncio.gather(*tasks)
